package com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_resource;

import com.hz.monitoring.framework.common.pojo.CommonResult;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.excel.core.util.ExcelUtils;
import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_resource.vo.*;
import com.hz.monitoring.module.hardware.convert.mcu_tenant_resource.McuTenantResourceConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.mcu_tenant_resource.McuTenantResourceDO;
import com.hz.monitoring.module.hardware.service.mcu_tenant_resource.McuTenantResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.hz.monitoring.framework.common.pojo.CommonResult.success;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - MCU租户配置")
@RestController
@RequestMapping("/hardware/mcu-tenant-resource")
@Validated
public class McuTenantResourceController {

    @Resource
    private McuTenantResourceService mcuTenantResourceService;

    @PostMapping("/create")
    @ApiOperation("创建MCU租户配置")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-resource:create')")
    public CommonResult<Long> createMcuTenantResource(@Valid @RequestBody McuTenantResourceCreateReqVO createReqVO) {
        return success(mcuTenantResourceService.createMcuTenantResource(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新MCU租户配置")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-resource:update')")
    public CommonResult<Boolean> updateMcuTenantResource(@Valid @RequestBody McuTenantResourceUpdateReqVO updateReqVO) {
        mcuTenantResourceService.updateMcuTenantResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除MCU租户配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-resource:delete')")
    public CommonResult<Boolean> deleteMcuTenantResource(@RequestParam("id") Long id) {
        mcuTenantResourceService.deleteMcuTenantResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得MCU租户配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-resource:query')")
    public CommonResult<McuTenantResourceRespVO> getMcuTenantResource(@RequestParam("id") Long id) {
        McuTenantResourceDO mcuTenantResource = mcuTenantResourceService.getMcuTenantResource(id);
        return success(McuTenantResourceConvert.INSTANCE.convert(mcuTenantResource));
    }

    @GetMapping("/list")
    @ApiOperation("获得MCU租户配置列表")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-resource:query')")
    public CommonResult<List<McuTenantResourceRespVO>> getMcuTenantResourceList(McuTenantResourceListReqVO listReqVO) {
        List<McuTenantResourceDO> list = mcuTenantResourceService.getMcuTenantResourceList(listReqVO);
        return success(McuTenantResourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得MCU租户配置分页")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-resource:query')")
    public CommonResult<PageResult<McuTenantResourceRespVO>> getMcuTenantResourcePage(@Valid McuTenantResourcePageReqVO pageVO) {
        PageResult<McuTenantResourceDO> pageResult = mcuTenantResourceService.getMcuTenantResourcePage(pageVO);
        return success(McuTenantResourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出MCU租户配置 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-resource:export')")
    @OperateLog(type = EXPORT)
    public void exportMcuTenantResourceExcel(@Valid McuTenantResourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<McuTenantResourceDO> list = mcuTenantResourceService.getMcuTenantResourceList(exportReqVO);
        // 导出 Excel
        List<McuTenantResourceExcelVO> datas = McuTenantResourceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "MCU租户配置.xls", "数据", McuTenantResourceExcelVO.class, datas);
    }

}
